/****************************************************************************
**
** ServerGump.cpp
**
** Copyright (C) August 2014 Hotride
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
*****************************************************************************
*/
//---------------------------------------------------------------------------
#include "stdafx.h"
//---------------------------------------------------------------------------
TServerGump::TServerGump()
{
	UO->m_Font->GenerateA(9, m_TextureSelect, "Select which shard to play on:", 0x0481);
	
	UO->m_Font->GenerateA(9, m_TextureSort, "Sort by:", 0x0481);
}
//---------------------------------------------------------------------------
TServerGump::~TServerGump()
{
	m_TextureSelect.Clear();
	m_TextureSort.Clear();
}
//---------------------------------------------------------------------------
void TServerGump::Init()
{
	char buf[128] = {0};
	sprintf(buf, "Ultima Online - %s", UO->GetAccount().c_str());

	SetWindowTextA(g_hWnd, buf);
}
//---------------------------------------------------------------------------
int TServerGump::Render(bool mode)
{
	DWORD ticks = GetTickCount();

	if (g_LastRenderTime > ticks)
	{
		if (mode || !g_SelectGumpObjects) return 0;
	}
	if (mode)
	{
		int CanSelectedButton = g_LastSelectedObject;

		int CanPressedButton = 0;
		if (g_LeftMouseDown && g_LastObjectLeftMouseDown == g_LastSelectedObject) CanPressedButton = g_LastObjectLeftMouseDown;
	
		g_LastRenderTime = ticks + g_FrameDelay;

		UO->m_GL.BeginDraw();
		
		UO->DrawGump(0x0588, 0, 0, 0, 640, 480); //Main Gump background
		UO->DrawGump(0x157C, 0, 0, 0); //Main Gump
		UO->DrawGump(0x15A0, 0, 0, 4); //Main Gump Notes
	
		WORD GumpID = 0x1589 + (int)(CanSelectedButton == 1); //X gump /lighted
		if (CanPressedButton == 1) GumpID = 0x158B; //X gump (pressed)
		UO->DrawGump(GumpID, 0, 555, 4);
		
		GumpID = 0x15A1 + (int)(CanSelectedButton == 2); //< gump /lighted
		if (CanPressedButton == 2) GumpID = 0x15A3; //< gump pressed
		UO->DrawGump(GumpID, 0, 586, 445); //< gump

		GumpID = 0x15A4 + (int)(CanSelectedButton == 3); //> gump /lighted
		if (CanPressedButton == 3) GumpID = 0x15A6; //> gump pressed
		UO->DrawGump(GumpID, 0, 610, 445); //> gump
		
		m_TextureSelect.Draw(233, 62);

		UO->DrawResizepicGump(0x0DAC, 150, 90, 380, 271); //Server list field
		
		//Server list scroll bar:
		UO->DrawGump(0x00FF, 0, 530, 91); //^ background
		//UO->DrawGump(0x0101, 0, 530, 328); //v background
		UO->DrawGump(0x0100, 0, 530, 111, 0, 240); //background
		UO->DrawGump(0x00FA, 0, 530, 91); //^
		UO->DrawGump(0x00FE, 0, 531, 112); //bar
		UO->DrawGump(0x00FC, 0, 530, 338); //v
		
		IFOR(i, 0, UO->m_ServerList->GetCount())
		{
			WORD FColor = 0x034F;
			if (g_LastSelectedObject == (8 + i)) FColor = 0x0021;

			UO->m_Font->DrawA(5, UO->m_ServerList->GetName(i).c_str(), FColor, 224, 100 + (i * 25));
		}
		
		m_TextureSort.Draw(153, 368);
		
		GumpID = 0x093B + (int)(CanSelectedButton == 4 ? 2 : 0); //TimeZone button /lighted
		if (CanPressedButton == 4) GumpID = 0x093C; //TimeZone button pressed
		UO->DrawGump(GumpID, 0, 230, 366); //TimeZone button

		GumpID = 0x093E + (int)(CanSelectedButton == 5 ? 2 : 0);  //Full button /lighted
		if (CanPressedButton == 5) GumpID = 0x093F; //Full button pressed
		UO->DrawGump(GumpID, 0, 338, 366); //Full button

		GumpID = 0x0941 + (int)(CanSelectedButton == 6 ? 2 : 0); //Connection button /lighted
		if (CanPressedButton == 6) GumpID = 0x0942; //Connection button pressed
		UO->DrawGump(GumpID, 0, 446, 366); //Connection button

		UO->DrawGump(0x0589, 0, 150, 390); //Earth container

		GumpID = 0x15E8 + (int)(CanSelectedButton == 7); //Earth button /lighted
		if (CanPressedButton == 7) GumpID = 0x15EA; //Earth button pressed
		UO->DrawGump(GumpID, 0, 160, 400); //Earth button
		
		if (UO->m_ServerList->GetCount() > 0) UO->m_Font->DrawA(9, UO->m_ServerList->GetName(0).c_str(), 0x0481, 243, 420);
		
		if (g_LastSelectedObject == 1) UO->SetTransparentHint(L"Quit Ultima Online", 80.0f);
		else if (g_LastSelectedObject == 2) UO->SetTransparentHint(L"Preveous screen");
		else if (g_LastSelectedObject == 3) UO->SetTransparentHint(L"Next screen");
		else if (g_LastSelectedObject == 7) UO->SetTransparentHint(L"Select first server in list", 100.0f);
		else if (g_LastSelectedObject >= 8)
		{
			String sstr("Connect to '" + UO->m_ServerList->GetName(g_LastSelectedObject - 8) + "' server");
			String ustr(sstr.ToUnicode());
			UO->SetTransparentHint(ustr.w_str(), 100.0f);
		}

		UO->DrawCursor(0x2073); //Main Gump mouse cursor

		UO->m_GL.EndDraw();
	}
	else
	{
		g_LastSelectedObject = 0;
		g_LastObjectType = SOT_NO_OBJECT;

		if (UO->GumpPixelsInXY(0x1589, 555, 4))
		{
			g_LastSelectedObject = 1; //X gump
		}
		else if (UO->GumpPixelsInXY(0x15A1, 586, 445))
		{
			g_LastSelectedObject = 2; //< gump
		}
		else if (UO->GumpPixelsInXY(0x15A4, 610, 445))
		{
			g_LastSelectedObject = 3; //> gump
		}
		else if (UO->GumpPixelsInXY(0x093B, 230, 366))
		{
			g_LastSelectedObject = 4; //TimeZone button
		}
		else if (UO->GumpPixelsInXY(0x093E, 338, 366))
		{
			g_LastSelectedObject = 5; //Full button
		}
		else if (UO->GumpPixelsInXY(0x0941, 446, 366))
		{
			g_LastSelectedObject = 6; //Connection button
		}
		else if (UO->GumpPixelsInXY(0x15E8, 160, 400))
		{
			g_LastSelectedObject = 7; //Earth button
		}
		else
		{
			IFOR(i, 0, UO->m_ServerList->GetCount())
			{
				RECT rc = {0, 0, 300, 25};
				POINT p = {g_MouseX - 224, g_MouseY - (100 + (i * 25))};

				if (PtInRect(&rc, p))
				{
					g_LastSelectedObject = (8 + i);
				}
			}
		}

		return g_LastSelectedObject;
	}
	return 0;
}
//---------------------------------------------------------------------------
void TServerGump::ParseLeftMouseDown()
{
	Render(false);

	g_DroppedLeftMouseX = g_MouseX;
	g_DroppedLeftMouseY = g_MouseY;
	g_LeftMouseDown = true;
	g_LastObjectLeftMouseDown = g_LastSelectedObject;

	if (g_LastObjectLeftMouseDown > 7) //Server selection
	{
		UO->ServerSelection(g_LastObjectLeftMouseDown - 8);
		return;
	}
}
//---------------------------------------------------------------------------
void TServerGump::ParseLeftMouseUp()
{
	Render(false);

	g_LeftMouseDown = false;
	g_DroppedLeftMouseX = 0;
	g_DroppedLeftMouseY = 0;

	if (g_LastSelectedObject == 0 || g_LastSelectedObject != g_LastObjectLeftMouseDown)
	{
		g_LastObjectLeftMouseDown = 0;
		return;
	}

	if (g_LastObjectLeftMouseDown == 1) //x button
	{
		PostQuitMessage(0);
		if (UO != NULL) delete UO;
		UO = NULL;
		return;
	}
	else if (g_LastObjectLeftMouseDown == 2) //< button
	{
		UO->Disconnect();
		UO->InitGump(GS_MAIN);
	}
	else if (g_LastObjectLeftMouseDown == 3 || g_LastObjectLeftMouseDown == 7) //> button
	{
		UO->ServerSelection(0);
	}

	g_LastObjectLeftMouseDown = 0;
}
//---------------------------------------------------------------------------
void TServerGump::ParseRightMouseDown()
{
}
//---------------------------------------------------------------------------
void TServerGump::ParseRightMouseUp()
{
}
//---------------------------------------------------------------------------
bool TServerGump::ParseLeftMouseDoubleClick()
{
	return false;
}
//---------------------------------------------------------------------------
bool TServerGump::ParseRightMouseDoubleClick()
{
	return false;
}
//---------------------------------------------------------------------------
void TServerGump::ParseMouseWheel(MOUSE_WHEEL_STATE state)
{
}
//---------------------------------------------------------------------------
void TServerGump::ParseCharPress(WPARAM wparam, LPARAM lparam)
{
}
//---------------------------------------------------------------------------
void TServerGump::ParseKeyPress(WPARAM wparam, LPARAM lparam)
{
	switch (wparam)
	{
		case VK_RETURN:
		{
			UO->ServerSelection(0);
			return;
		}
		default: break;
	}
}
//---------------------------------------------------------------------------